package com.bydq.patrolsystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bydq.patrolsystem.entity.PsInspectiontaskDetail;
import com.bydq.patrolsystem.entity.PsInspectiontaskMain;
import com.bydq.patrolsystem.entity.PsPoleInfo;
import com.bydq.patrolsystem.entity.PsSystemconfig;
import com.bydq.patrolsystem.service.PsInspectiontaskDetailService;
import com.bydq.patrolsystem.service.PsInspectiontaskMainService;
import com.bydq.patrolsystem.service.PsSystemconfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("/flaw")
public class FlawTypeController {

    @Autowired
    private PsSystemconfigService psSystemconfigService;

    @Autowired
    private PsInspectiontaskDetailService psInspectiontaskDetailService;

    @Autowired
    private PsInspectiontaskMainService psInspectiontaskMainService;

    /**
     * 页面跳转至flaw_manage.xml
     * @return
     */
    @RequestMapping("/manage")
    public String flawManage( ){
        return "flaw_manage";
    }

    /**
     * 页面跳转至flaw_confirm_save.xml
     * @return
     */
    @RequestMapping("/flaw_confirm_save")
    public String flaw_confirm_save(Model m,Long id){
        m.addAttribute("id",id);
        m.addAttribute("bugLevelList",bugLevelList());
        return "flaw_confirm_save";
    }

    /**
     * 确认缺陷级别，修改对应数据
     * @param
     * @return
     */
    @RequestMapping("/confirm_save")
    @ResponseBody
    public boolean confirm_save(Long id,Long bugLevel){
//        System.out.println("id:"+id);
//        System.out.println("bugLevel:"+bugLevel);
        PsInspectiontaskDetail psInspectiontaskDetail = psInspectiontaskDetailService.getById(id);
        psInspectiontaskDetail.setIsBugLevel(1);
        psInspectiontaskDetail.setBugLevel(bugLevel);
        boolean flag = false;
        flag = psInspectiontaskDetailService.updateById(psInspectiontaskDetail);
        return flag;
    }



    /**
     * 页面跳转至flaw_confirm.xml
     * @return
     */
    @RequestMapping("/confirm")
    public String flawConfirm(){
        return "flaw_confirm";
    }

    /**
     * 查询缺陷管理列表
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("/list")
    public Map<String,Object> flawList(Integer page,Integer limit){
        page = page == null ? 1 : page;
        limit = limit == null ? 3 : limit;
        String configCode = "BUG_TYPE";
        IPage<PsSystemconfig> pages = new Page<>(page,limit);
        QueryWrapper<PsSystemconfig> qw=new QueryWrapper<>();
        qw.eq("configCode",configCode);
        psSystemconfigService.page(pages,qw);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",pages.getTotal());
        map.put("data", pages.getRecords());
//        System.out.println(map.toString());
        return map;
    }

    /**
     * 查询缺陷类型下拉列表集合
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("/flawTypeList")
    public List<PsSystemconfig> flawTypeList(){
        String configCode = "BUG_TYPE";
        QueryWrapper<PsSystemconfig> qw=new QueryWrapper<>();
        qw.eq("configCode",configCode);
//        System.out.println(psSystemconfigService.list(qw));
        return psSystemconfigService.list(qw);
    }

    /**
     * 查询缺陷级别下拉列表集合
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("/bugLevelList")
    public List<PsSystemconfig> bugLevelList(){
        String configCode = "BUG_LEVEL";
        QueryWrapper<PsSystemconfig> qw=new QueryWrapper<>();
        qw.eq("configCode",configCode);
//        System.out.println(psSystemconfigService.list(qw));
        return psSystemconfigService.list(qw);
    }


    /**
     * 查询缺陷确认列表页
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("/confirmList")
    public Map<String,Object> poleList(String inspectionTaskCode,String lineCode, String poleCode,
                                       String discovererName, Long bugType,Long bugLevel,
                                       String startDate,String endDate,
                                       Integer page, Integer limit) {
//        System.out.println("inspectionTaskCode:"+inspectionTaskCode);
//        System.out.println("lineCode:"+lineCode);
//        System.out.println("poleCode:"+poleCode);
//        System.out.println("discovererName:"+discovererName);
        List<Long> taskIdlist = new ArrayList<Long>();
       if(inspectionTaskCode!=null&&inspectionTaskCode!=""){
            QueryWrapper<PsInspectiontaskMain> qw=new QueryWrapper<>();
            qw.like("inspectionTaskCode",inspectionTaskCode);
            List<PsInspectiontaskMain> psInspectiontaskMainList =psInspectiontaskMainService.list(qw);
            for(int i =0;i<psInspectiontaskMainList.size();i++){
                taskIdlist.add(psInspectiontaskMainList.get(i).getId());
            }
        }
        IPage<PsInspectiontaskDetail> pages = psInspectiontaskDetailService.selectInspectionTaskListByPaging(inspectionTaskCode,taskIdlist,lineCode, poleCode,
                discovererName, bugType,bugLevel,
                startDate,endDate,
                page, limit);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", pages.getTotal());
        map.put("data", pages.getRecords());
//        System.out.println(map.toString());
        return map;
    }

    /**
     * 页面跳转至flaw_add.xml
     * @return
     */
    @RequestMapping("/flaw_add")
    public String flaw_add(){
        return "flaw_add";
    }

    /**
     * 页面跳转至flaw_edit.xml
     * @return
     */
    @RequestMapping("/flaw_edit")
    public String flaw_eidt(Integer id,Model m){
//        System.out.println("id:"+id);
        if(id!=null){
            //修改
            m.addAttribute("PsSystemconfig",psSystemconfigService.getById(id));
        }
        return "flaw_edit";
    }

    /**
     * 添加修改缺陷
     * @param
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public boolean save(PsSystemconfig psSystemconfig, HttpSession session){
//        System.out.println("flawCode:"+psSystemconfig.getConfigValueName());
//        System.out.println("getIsStart:"+psSystemconfig.getIsStart());
        boolean flag = false;
        if(psSystemconfig.getId()!=null){
//            System.out.println("修改");
            flag = psSystemconfigService.updateById(psSystemconfig);
        }else{
//            System.out.println("添加");
            psSystemconfig.setConfigCode("BUG_TYPE");
            psSystemconfig.setConfigName("缺陷类型");
            flag = psSystemconfigService.save(psSystemconfig);
        }
        return flag;
    }

    /**
     * 删除缺陷
     * @param
     * @return
     */
    @RequestMapping("/del")
    @ResponseBody
    public boolean del(Integer id) {
//        System.out.println("id:"+id);
        boolean flag = psSystemconfigService.removeById(id);
//        System.out.println("flag:"+flag);
        return flag;
    }

    /**
     * 验证缺陷类型的configValueId是否存在,存在返回false，不存在返回true
     * @param
     * @return
     */
    @RequestMapping("/seclectByconfigValueId")
    @ResponseBody
    public boolean seclectByconfigValueId(@RequestBody PsSystemconfig psSystemconfig) {
//        System.out.println("configValueId:"+psSystemconfig.getConfigValueId());
        boolean flag = true;
        String configCode = "BUG_TYPE";
        QueryWrapper<PsSystemconfig> qw=new QueryWrapper<>();
        qw.eq("configCode",configCode);
        qw.eq("configValueId",psSystemconfig.getConfigValueId());
        List<PsSystemconfig> list = psSystemconfigService.list(qw);
        if(list.size()>0){
            flag=false;
        }
//        System.out.println("flag:"+flag);
        return flag;

    }


}



